/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.apache.log4j.Logger;

/**
 * A table model for updating non matching information
 * 
 * @see JDialogEditNonMatchingInfo
 * @see NonMatchingInfo
 * @author stl
 */
public class NonMatchingInfoTableModel extends AbstractTableModel {
		
    private static Logger logger = Logger.getLogger(NonMatchingInfoTableModel.class); 
	
	private List<NonMatchingInfo> nonMatchingInfo;	
	private String columnName ="";
	private boolean dirty =false;	
	
	public boolean isDirty() {
		return dirty;
	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}
	
	public void setColumnName( String columnName ) {
		this.columnName =columnName;
	}
	
	public void setNonMatchingInfo( List<NonMatchingInfo> nonMatchingInfo ) {
		this.nonMatchingInfo =nonMatchingInfo;
		
		setDirty( false );
		
        fireTableDataChanged();
	}
	
	public List<NonMatchingInfo> getNonMatchingInfo() {
		return nonMatchingInfo;
	}
										  
	public int getColumnCount() {
		return 1;
	}

	public int getRowCount() {
		return nonMatchingInfo !=null ? nonMatchingInfo.size() : 0;
	}

	public String getColumnName(int col) {
		return columnName;
	}

	public Object getValueAt(int row, int col) {
		NonMatchingInfo nmi =nonMatchingInfo.get( row );
		
		return nmi.getUpdatedVal();
	}
	
    public void setValueAt(Object value, int row, int col) {
		
		NonMatchingInfo nmi =nonMatchingInfo.get( row );
		nmi.setUpdatedVal( (String)value );
    	                    
        fireTableCellUpdated(row, col);
        
        setDirty( true );
    }	

	/*
	 * JTable uses this method to determine the default renderer/
	 * editor for each cell.  If we didn't implement this method,
	 * then the last column would contain text ("true"/"false"),
	 * rather than a check box.
	 */
	@SuppressWarnings("unchecked")
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	/*
	 * Don't need to implement this method unless your table's
	 * editable.
	 */
	public boolean isCellEditable(int row, int col) {
		return true; 
	}
     
}
